package com.example.utils;

import com.example.constant.PathConstant;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;

/**
 * @Author: 张比特
 * @Data: 2025/2/10 下午5:47
 * @Version: v1.0.0
 * @Description: TODO python执行工具
 **/
public class PythonExecuteUtil {

    /**
     * 进行YOLOv7标记
     * @param ScriptName
     * @param weights
     * @param source
     * @param target
     * @return
     */
    public static int executeYOLOv7Script(String ScriptName,String weights, String source, String target) {
        List<String> command = new ArrayList<>();
        command.add("python.exe");

        // 脚本执行参数
        command.add(ScriptName);  // Python脚本路径
        command.add("--weights");
        command.add(weights);
        command.add("--source");   // --source 参数
        command.add(source);      // source路径
        command.add("--project");  // --target 参数
        command.add(target);      // target路径

        ProcessBuilder builder = new ProcessBuilder(command);
        builder.redirectErrorStream(true);  // 错误数据标准输出
        Process process = null;

        try {
            process = builder.start();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);  // 输出Python脚本的执行结果
                }
            }

            int exitCode = process.waitFor();  // 等待Python脚本执行完毕
            if (exitCode != 0) {
                throw new RuntimeException("Python脚本退出代码：" + exitCode);
            }
            return exitCode;  // 返回退出代码
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException("执行失败", e);  // 处理执行过程中的异常
        } finally {
            if (process != null && process.isAlive()) {
                process.destroy();  // 销毁进程，防止进程未正确关闭
            }
        }
    }

    /**
     * 执行匹配脚本
     * @param ScriptName
     * @param source
     * @param target
     * @return
     */
    public static JSONArray executeFeatureExtractionScript(String ScriptName, String source, String target) {
        List<String> command = new ArrayList<>();
        command.add("python.exe");

        // 脚本执行参数
        command.add(ScriptName);  // Python脚本路径
        command.add("--source");
        command.add(source);
        command.add("--target");
        command.add(target);

        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = null;

        // 构建JSON类型
        List<JSONObject> jsonList = new ArrayList<>();

        try {
            process = builder.start();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                String line;
                while ((line = reader.readLine()) != null) {
//                    System.out.println(line);  // 输出Python脚本的执行结果
                    try {
                        // 获取JSON行
                        JSONObject obj = new JSONObject(line);
                        jsonList.add(obj);
                    } catch (JSONException e) {
                        // 忽略非JSON行
                    }
                }
            }

            int exitCode = process.waitFor();  // 等待Python脚本执行完毕
            if (exitCode != 0) {
                throw new RuntimeException("Python脚本退出代码：" + exitCode);
            }

            // 看看输出
            JSONArray resultArray = new JSONArray(jsonList);
//            System.out.println(resultArray.toString(2)); // 格式化输出
//            return exitCode;  // 返回退出代码
            return resultArray;
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException("执行失败", e);  // 处理执行过程中的异常
        } finally {
            if (process != null && process.isAlive()) {
                process.destroy();  // 销毁进程，防止进程未正确关闭
            }
        }
    }

}
